package com.linewow.xhyy.nihaonew.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.canyinghao.candialog.CanDialog;
import com.canyinghao.candialog.CanDialogInterface;
import com.linewow.xhyy.hrxb.DemoHelper;
import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.application.NiHaoApp;
import com.linewow.xhyy.nihaonew.base.BaseFragmentActivity;
import com.linewow.xhyy.nihaonew.entity.NiHaoGeneral;
import com.linewow.xhyy.nihaonew.event.MessageEvent;
import com.linewow.xhyy.nihaonew.fragment.TopicDetailFragment;
import com.linewow.xhyy.nihaonew.model.HttpModel;
import com.linewow.xhyy.nihaonew.net.api.NiHaoOkgo;
//import com.umeng.socialize.ShareAction;
//import com.umeng.socialize.UMAuthListener;
//import com.umeng.socialize.UMShareAPI;
//import com.umeng.socialize.UMShareListener;
//import com.umeng.socialize.bean.SHARE_MEDIA;
//import com.umeng.socialize.media.UMImage;
//import com.umeng.socialize.shareboard.SnsPlatform;
//import com.umeng.socialize.utils.ShareBoardlistener;

import org.simple.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by LXR on 2016/9/6.
 */
public class TopicDetailActivity extends BaseFragmentActivity {
    @Bind(R.id.toolbar_tag_list_title)
    TextView toolbarTagListTitle;
    @Bind(R.id.toolbar_tag_list_left)
    ImageView toolbarTagListLeft;
    @Bind(R.id.toolbar_find_right)
    ImageView toolbarFindRight;
    @Bind(R.id.toolbar_tag_list_toolbar)
    Toolbar toolbarTagListToolbar;
    @Bind(R.id.topic_detail_frame)
    FrameLayout topicDetailFrame;
    private String TAG = "TopicDetailActivity";
    private String key="msg";
    private String tid;
    private TopicDetailFragment topicDetailFragment;
    private FragmentManager fm;
//    private UMImage umImage;
    private ClipboardManager clipboardManager;
    private String []arrString;

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_topic_detail);
        ButterKnife.bind(this);
        toolbarTagListTitle.setText(getResources().getString(R.string.title_topic_detail));
        toolbarFindRight.setImageResource(R.mipmap.icon_menu_white);
        fm=getSupportFragmentManager();
//        umImage = new UMImage(TopicDetailActivity.this, R.mipmap.icon_nihao);
        clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

    }

    @Override
    public void initList() {
        super.initList();
    }

    @Override
    public void initData() {
        super.initData();
        tid=getIntent().getStringExtra(key);
        topicDetailFragment=new TopicDetailFragment();
        Bundle bundle=new Bundle();
        bundle.putString(key,tid);
        topicDetailFragment.setArguments(bundle);
        fm.beginTransaction().add(R.id.topic_detail_frame,topicDetailFragment).commit();
        arrString=new String[]{getString(R.string.menu_report_ad),getString(R.string.menu_report_illegal),getString(R.string.menu_report_sex),getString(R.string.menu_report_deceit),getString(R.string.menu_report_other)};
    }


    @OnClick({R.id.toolbar_tag_list_left, R.id.toolbar_find_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_tag_list_left:
                finish();
                break;
            case R.id.toolbar_find_right:
                if(topicDetailFragment.selfFlag){
                    AlertView.Builder builder=new AlertView.Builder().setContext(TopicDetailActivity.this)
                            .setStyle(AlertView.Style.ActionSheet)
                            .setTitle(getString(R.string.menu_more))
                            .setCancelText(getString(R.string.menu_cancel));
                    builder.setOthers(new String[]{getString(R.string.menu_share),getString(R.string.menu_report),getString(R.string.menu_link),getString(R.string.menu_delete)});
                    builder.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(Object o, int position) {
                            switch (position){
                                case 0:
//                                    shareUmeng();
                                    break;
                                case 1:
                                    showReportView();
                                    break;
                                case 2:
                                    Log.e(TAG,"3");
                                    clipClick();
                                    break;
                                case 3:
                                    deleteTopicInfo();
                                    break;
                            }
                        }
                    }).build().show();
                }else{
                    AlertView.Builder builder=new AlertView.Builder().setContext(TopicDetailActivity.this)
                            .setStyle(AlertView.Style.ActionSheet)
                            .setTitle(getString(R.string.menu_more))
                            .setCancelText(getString(R.string.menu_cancel));
                    builder.setOthers(new String[]{getString(R.string.menu_share),getString(R.string.menu_report),getString(R.string.menu_link)});
                    builder.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(Object o, int position) {
                            switch (position){
                                case 0:
//                                    shareUmeng();
                                    break;
                                case 1:
                                    showReportView();
                                    break;
                                case 2:
                                    clipClick();
                                    break;
                                case 3:
                                    break;
                            }
                        }
                    }).build().show();
                }

                break;
        }
    }

    private void clipClick() {
        ClipData clipData=ClipData.newPlainText("topic",(HttpModel.HOMETOPICCLIP+tid));
        clipboardManager.setPrimaryClip(clipData);
        showToast(getString(R.string.clip_success));
    }

    private void deleteTopicInfo() {

        new CanDialog.Builder(TopicDetailActivity.this)
                .setTitle(getString(R.string.menu_notice))
                .setMessage(getString(R.string.message_delete_confirm))
                .setCircularRevealAnimator(CanDialog.CircularRevealStatus.TOP_LEFT)
                .setPositiveButton(getString(R.string.widget_sure), true, new CanDialogInterface.OnClickListener() {
                    @Override
                    public void onClick(CanDialog dialog, int checkItem, CharSequence text, boolean[] checkItems) {
                        NiHaoOkgo.delTopic(tid)
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Action1<NiHaoGeneral<String>>() {
                                    @Override
                                    public void call(NiHaoGeneral<String> stringNiHaoGeneral) {
                                        if(stringNiHaoGeneral.status){
                                            EventBus.getDefault().post(new MessageEvent(),"refresh");
                                            Intent intent=new Intent(TopicDetailActivity.this,FrameActivity.class);
                                            startActivity(intent);
                                        }else{
                                            showSnack(getString(R.string.message_delete_failed));
                                        }
                                    }
                                });
                    }
                })
                .setNegativeButton(getString(R.string.widget_cancel),true,null)
                .show();

    }


    private void showReportView() {
        new CanDialog.Builder(TopicDetailActivity.this)
                .setTitle(getString(R.string.menu_report))
                .setSingleChoiceItems(arrString, 1, new CanDialogInterface.OnClickListener() {
                    @Override
                    public void onClick(CanDialog dialog, int position, CharSequence text, boolean[] checkitems) {
                    }
                })
                .setNegativeButton(getString(R.string.widget_cancel), true, new CanDialogInterface.OnClickListener() {
                    @Override
                    public void onClick(CanDialog dialog, int checkItem, CharSequence text, boolean[] checkItems) {

                    }
                })
                .setPositiveButton(getString(R.string.widget_sure), true, new CanDialogInterface.OnClickListener() {
                    @Override
                    public void onClick(CanDialog dialog, int checkItem, CharSequence text, boolean[] checkItems) {

                        if(NiHaoApp.flag){
                            requestReportInfo(checkItem);
                        }else{
                            DemoHelper.getInstance().onConnectionConflict();
                        }

                    }
                })
                .setTileAnimator()
                .setCancelable(true)
                .setCircularRevealAnimator(CanDialog.CircularRevealStatus.TOP_LEFT)
                .show();
    }

    private void requestReportInfo(int text) {
        NiHaoOkgo.reportInfo(tid,arrString[text])
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<NiHaoGeneral<String>>() {
                    @Override
                    public void call(NiHaoGeneral<String> stringNiHaoGeneral) {

                        Log.e(TAG,"举报成功了"+stringNiHaoGeneral);
                        if(stringNiHaoGeneral.status){
//                            Toast.makeText(UserMineMainFragment.this.getActivity(), "举报成功",
//                                    Toast.LENGTH_SHORT).show();
                            showSnack(getString(R.string.report_success));
                        }else{
                            showSnack(getString(R.string.report_failed));
                        }
                    }
                });

    }


//    private void shareUmeng() {
//        new ShareAction(this).setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.TENCENT)
//                .setShareboardclickCallback(shareBoardlistener)
//                .open();
//    }
//    private ShareBoardlistener shareBoardlistener = new ShareBoardlistener() {
//
//        @Override
//        public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
//            if (share_media == SHARE_MEDIA.TENCENT) {
//                mShareAPI.doOauthVerify(TopicDetailActivity.this, SHARE_MEDIA.TENCENT, umAuthListener);
//            } else if (share_media == SHARE_MEDIA.QQ) {
//                new ShareAction(TopicDetailActivity.this).setPlatform(share_media).setCallback(umShareListener)
//                        .withText(getString(R.string.app_name))
//                        .withTargetUrl(HttpModel.SHAREURL)
//                        .share();
//            } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
//                new ShareAction(TopicDetailActivity.this).setPlatform(share_media).setCallback(umShareListener)
//                        .withText(getString(R.string.message_share_content))
//                        .withMedia(umImage)
//                        .withTitle(getString(R.string.message_share_content))
//                        .withTargetUrl(HttpModel.SHAREURL)
//                        .share();
//            } else {
//                new ShareAction(TopicDetailActivity.this).setPlatform(share_media).setCallback(umShareListener)
//                        .withMedia(umImage)
//                        .withText(getString(R.string.message_share_content))
//                        .withTargetUrl(HttpModel.SHAREURL)
//                        .share();
//            }
//        }
//    };

//    private UMShareListener umShareListener = new UMShareListener() {
//        @Override
//        public void onResult(SHARE_MEDIA platform) {
//            Toast.makeText(TopicDetailActivity.this, platform + getString(R.string.message_share_success), Toast.LENGTH_SHORT).show();
//        }
//
//        @Override
//        public void onError(SHARE_MEDIA platform, Throwable t) {
//            Toast.makeText(TopicDetailActivity.this, platform + getString(R.string.message_share_error), Toast.LENGTH_SHORT).show();
//        }
//
//        @Override
//        public void onCancel(SHARE_MEDIA platform) {
//            Toast.makeText(TopicDetailActivity.this, platform + getString(R.string.message_share_cancel), Toast.LENGTH_SHORT).show();
//        }
//    };

//    private UMAuthListener umAuthListener = new UMAuthListener() {
//        @Override
//        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
//            Log.e(TAG, "认证成功了");
//            new ShareAction(TopicDetailActivity.this).setPlatform(SHARE_MEDIA.TENCENT).setCallback(umShareListener)
//                    .withText(getResources().getString(R.string.app_name))
//                    .withMedia(umImage)
//                    .withText(HttpModel.SHAREURL)
//                    .withTargetUrl(HttpModel.SHAREURL)
//                    .share();
//        }
//
//        @Override
//        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
//            Log.e(TAG, "认证失败了");
//        }
//
//        @Override
//        public void onCancel(SHARE_MEDIA platform, int action) {
//            Log.e(TAG, "认证取消了");
//        }
//    };

    public boolean getEditFlag(){
        return topicDetailFragment.getEditFlag();
    }
}